{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "rFiCyWQ-NC5D"
   },
   "source": [
    "# Multiple Layer LSTM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 2688,
     "status": "ok",
     "timestamp": 1597801190558,
     "user": {
      "displayName": "Tensorflow2 Material",
      "photoUrl": "",
      "userId": "10677934629717325810"
     },
     "user_tz": 420
    },
    "id": "Y20Lud2ZMBhW",
    "outputId": "16ed16fe-4b5a-466e-e500-0d5fe25151d2"
   },
   "outputs": [],
   "source": [
    "from __future__ import absolute_import, division, print_function, unicode_literals\n",
    "\n",
    "\n",
    "import tensorflow_datasets as tfds\n",
    "import tensorflow as tf\n",
    "print(tf.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 401,
     "status": "ok",
     "timestamp": 1597806033076,
     "user": {
      "displayName": "Tensorflow2 Material",
      "photoUrl": "",
      "userId": "10677934629717325810"
     },
     "user_tz": 420
    },
    "id": "uAU8g7C0MPZE",
    "outputId": "a72ff07c-087f-42f2-87f0-b39244c23e00"
   },
   "outputs": [],
   "source": [
    "import tensorflow_datasets as tfds\n",
    "import tensorflow as tf\n",
    "print(tf.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 560,
     "status": "ok",
     "timestamp": 1597807528645,
     "user": {
      "displayName": "Tensorflow2 Material",
      "photoUrl": "",
      "userId": "10677934629717325810"
     },
     "user_tz": 420
    },
    "id": "AW-4Vo4TMUHb"
   },
   "outputs": [],
   "source": [
    "# Get the data\n",
    "# dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True, as_supervised=True)\n",
    "# train_dataset, test_dataset = dataset['train'], dataset['test']\n",
    "dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True, as_supervised=True)\n",
    "train_dataset, test_dataset = dataset['train'].take(4000), dataset['test'].take(1000)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 361,
     "status": "ok",
     "timestamp": 1597807530234,
     "user": {
      "displayName": "Tensorflow2 Material",
      "photoUrl": "",
      "userId": "10677934629717325810"
     },
     "user_tz": 420
    },
    "id": "L11bIR6-PKvs"
   },
   "outputs": [],
   "source": [
    "tokenizer = info.features['text'].encoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 575
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 363,
     "status": "ok",
     "timestamp": 1597807532206,
     "user": {
      "displayName": "Tensorflow2 Material",
      "photoUrl": "",
      "userId": "10677934629717325810"
     },
     "user_tz": 420
    },
    "id": "e4pF2oigYbpk",
    "outputId": "778c87da-8c0f-4d8f-88a2-dc75821ae113"
   },
   "outputs": [],
   "source": [
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 521,
     "status": "ok",
     "timestamp": 1597807535456,
     "user": {
      "displayName": "Tensorflow2 Material",
      "photoUrl": "",
      "userId": "10677934629717325810"
     },
     "user_tz": 420
    },
    "id": "ffvRUI0_McDS"
   },
   "outputs": [],
   "source": [
    "BUFFER_SIZE = 100\n",
    "BATCH_SIZE = 100\n",
    "\n",
    "train_dataset = train_dataset.shuffle(BUFFER_SIZE).take(1000)\n",
    "train_dataset = train_dataset.padded_batch(BATCH_SIZE)\n",
    "test_dataset = test_dataset.padded_batch(BATCH_SIZE).take(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1396,
     "status": "ok",
     "timestamp": 1597808284909,
     "user": {
      "displayName": "Tensorflow2 Material",
      "photoUrl": "",
      "userId": "10677934629717325810"
     },
     "user_tz": 420
    },
    "id": "jo1jjO3vn0jo"
   },
   "outputs": [],
   "source": [
    "vocab_size = 1000\n",
    "model = tf.keras.Sequential([\n",
    "    tf.keras.layers.Embedding(tokenizer.vocab_size, 8),\n",
    "    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(8, return_sequences=True)),\n",
    "    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(8)),\n",
    "    tf.keras.layers.Dense(16, activation='relu'),\n",
    "    tf.keras.layers.Dense(1, activation='sigmoid')\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 329
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 385,
     "status": "ok",
     "timestamp": 1597808286338,
     "user": {
      "displayName": "Tensorflow2 Material",
      "photoUrl": "",
      "userId": "10677934629717325810"
     },
     "user_tz": 420
    },
    "id": "QKI5dfPgMioL",
    "outputId": "b1a9174a-ef8a-493e-b1f6-68558f5b89a2"
   },
   "outputs": [],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 352,
     "status": "ok",
     "timestamp": 1597808289073,
     "user": {
      "displayName": "Tensorflow2 Material",
      "photoUrl": "",
      "userId": "10677934629717325810"
     },
     "user_tz": 420
    },
    "id": "Uip7QOVzMoMq"
   },
   "outputs": [],
   "source": [
    "model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 384
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 884872,
     "status": "ok",
     "timestamp": 1597809178544,
     "user": {
      "displayName": "Tensorflow2 Material",
      "photoUrl": "",
      "userId": "10677934629717325810"
     },
     "user_tz": 420
    },
    "id": "7mlgzaRDMtF6",
    "outputId": "0b4328a1-f082-4b5b-93de-49c4001657e3"
   },
   "outputs": [],
   "source": [
    "NUM_EPOCHS = 10\n",
    "history = model.fit(train_dataset, epochs=NUM_EPOCHS, validation_data=test_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Mp1Z7P9pYRSK"
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "def plot_graphs(history, string):\n",
    "  plt.plot(history.history[string])\n",
    "  plt.plot(history.history['val_'+string])\n",
    "  plt.xlabel(\"Epochs\")\n",
    "  plt.ylabel(string)\n",
    "  plt.legend([string, 'val_'+string])\n",
    "  plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "R_sX6ilIM515"
   },
   "outputs": [],
   "source": [
    "plot_graphs(history, 'accuracy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "RFEXtKtqNARB"
   },
   "outputs": [],
   "source": [
    "plot_graphs(history, 'loss')"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "Part 15 - Lesson 1b.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
